const {
    ordersModel
} = require('../models/ordersModels');
const {
    customersModel
} = require('../models/customersModels');

const {
    productsModel
} = require('../models/productsModels');

module.exports.getGetOrdersList = async params => {
    return await ordersModel.find(params)
        .populate("customersId productsId salesId");
}

module.exports.postOrdersAdd = async params => {
    return await ordersModel.create(params);

}

module.exports.applyOrders = async params => {
    return await ordersModel.updateOne({
        _id: params
    }, {
        termination: 2
    });

    // return await ordersModel.updateOne();
} 



    //获取单个订单信息
module.exports. getOrdersOne = async params => {
    const data=await ordersModel.find({_id:params }).populate(  "claimsId customersId productsId salesId");
    return data
} 


module.exports.deleteOrders = async params => {
    return await ordersModel.deleteOne({
        _id: params
    });
}


module.exports.getOrdersId = async params => {
    const {
        salesId,
        customersId,
        productsId
    } = params
    // searchValue , salesId
    //1.假设输入的客户姓名：根据searchValue去查询客户集合，查询searchValue对应的客户id
    const customersIdData = await customersModel.find({
        name: {
            $regex: customersId,
            $options: '$i'
        },
        belongToSales: salesId
    }, '_id');
    // 2. 假设输入的产品姓名
    const productsIdData = await productsModel.find({
        name: {
            $regex: productsId,
            $options: '$i'
        }
    },"_id");
    // return customersIdData
    // return productsIdData
 return await ordersModel.find({
        $or: [{
                customersId: {
                    $in: customersIdData
                },
            },
            {
                productsId: {
                    $in: productsIdData
                }
            }
        ],
        salesId: salesId

    })   .populate("customersId productsId salesId");

}

module.exports.getOrdersSalesIdList = async params => {
    return await ordersModel.find(
        {
            salesId: params
        })   .populate("customersId productsId salesId");
}
